package com.gr.listener;

import com.gr.entity.Log;
import com.gr.service.ILogService;
import com.rabbitmq.client.Channel;
import org.apache.commons.lang3.SerializationUtils;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
public class LogListener {
    //利用消息队列实现日志入库，用户不在依赖日志模块
    @Autowired
    private ILogService logService;
    @RabbitListener(queues = "QA")
    public void receiveMsg(Message message, Channel channel) throws IOException {
        byte[] body = message.getBody();
        //byte转化log对象用反序列化工具
        Log log = (Log) SerializationUtils.deserialize(body);
        //调用业务类把日志插入到数据库表里面
        logService.save(log);
    }
}
